<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use App\User;

class UserFan extends Model
{
    //粉丝数
    public static function getFansCount($user_id)
    {
        $where = [
            ['follow_id', '=', $user_id],
            ['status', '=', 1],
        ];
        return self::where($where)->count();
    }

    //关注数
    public static function getFollowCount($user_id)
    {
        $where = [
            ['user_id', '=', $user_id],
            ['status', '=', 1],
        ];
        return self::where($where)->count(); //关注数
    }

    //是否互相关注
    public static function isEachFollow($user_id, $follow_id)
    {
        $where = [
            ['user_id', '=', $user_id],
            ['follow_id', '=', $follow_id],
            ['status', '=', 1],
        ];
        $fans = self::where($where)->first();
        if ($fans) {
            return 1;
        }
        return 0;
    }

    public static function getPageList($where = [], $order = 'id', $sort = 'desc')
    {
        $where[] = ['status', '=' , '1'];
        $fans = self::where($where)
            ->orderBy($order, $sort)
            ->paginate(10)
            ->toArray();
        foreach ($fans['data'] as $key => &$val) {
            $user = User::find($val['follow_id']);
            if ($user) {
                $val['user'] = $user->toArray();
            } else {
                unset($fans['data'][$key]);
            }
        }
        return $fans;
    }
}